/**
 *
 */
package com.example.mdteachingsys.stu.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xscj.domain.Message;
import com.xscj.domain.Student;
import com.xscj.service.MessageService;
import com.xscj.service.StuService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.HashMap;
import java.util.Map;

/**
 * @author leorain
 */
public class StudentMessageAddAction extends ActionSupport {

    /**
     *
     */
    private static final long serialVersionUID = -7652431825461314873L;

    @Autowired
    @Qualifier("stuServiceImpl")
    private StuService stuService;

    @Autowired
    @Qualifier("messageServiceImpl")
    private MessageService messageService;

    private String result;
    private String messageTitle;
    private String messageContent;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    @Override
    public String execute() throws Exception {
        boolean flag = false;
        ActionContext ctx = ActionContext.getContext();
        String userName = (String) ctx.getSession().get("userName");
        String userRole = (String) ctx.getSession().get("userRole");
        if (userName != null && userRole.equals("学生")) {
            Student student = stuService.getStudent(Integer.parseInt(userName));
            Message message = new Message();
            message.setMessageTitle(messageTitle);
            message.setMessageContent(messageContent);
            message.setUserId(userName);
            message.setUserName(student.getName());
            messageService.insertStudentMessage(message);
            flag = true;
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("flag", flag);
        JSONObject json = JSONObject.fromObject(map);
        result = json.toString();
        return SUCCESS;
    }

    public String getMessageTitle() {
        return messageTitle;
    }

    public void setMessageTitle(String messageTitle) {
        this.messageTitle = messageTitle;
    }

    public String getMessageContent() {
        return messageContent;
    }

    public void setMessageContent(String messageContent) {
        this.messageContent = messageContent;
    }
}
